package com.elai.overview.controller;

import java.util.ArrayList;
import java.util.List;

import com.elai.common.core.domain.model.LoginUser;
import com.elai.common.utils.SecurityUtils;
import com.elai.common.utils.ServletUtils;
import com.elai.overview.domain.MyOverviewPage;
import com.elai.overview.service.IMyOverviewPageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.elai.common.annotation.Log;
import com.elai.common.core.controller.BaseController;
import com.elai.common.core.domain.AjaxResult;
import com.elai.common.enums.BusinessType;
import com.elai.overview.domain.MyOverviewTeam;
import com.elai.overview.service.IMyOverviewTeamService;
import com.elai.common.utils.poi.ExcelUtil;
import com.elai.common.core.page.TableDataInfo;

/**
 * 我的概览Controller
 *
 * @author elai
 * @date 2021-08-29
 */
@RestController
@RequestMapping("/overview/team")
public class MyOverviewTeamController extends BaseController
{
    @Autowired
    private IMyOverviewTeamService myOverviewTeamService;

    @Autowired
    private IMyOverviewPageService myOverviewPageService;

    @PreAuthorize("@ss.hasPermi('overview:team:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyOverviewTeam myOverviewTeam)
    {
        LoginUser user= SecurityUtils.getLoginUser();
        myOverviewTeam.setUserId(user.getUser().getUserId());
        List<MyOverviewTeam> list = myOverviewTeamService.selectMyOverviewTeamList(myOverviewTeam);
        return getDataTable(list);
    }
    /**
     * 查询我的概览列表
     */
    @GetMapping("/getMyTeamlist")
    public AjaxResult getMyTeamlist(MyOverviewTeam myOverviewTeam)
    {
        LoginUser user= SecurityUtils.getLoginUser();
        myOverviewTeam.setUserId(user.getUser().getUserId());
        List<MyOverviewTeam> list = myOverviewTeamService.selectMyOverviewTeamList(myOverviewTeam);
        list.stream().forEach(team->{
            team.setLabel(team.getTeamName());
            MyOverviewPage page=new MyOverviewPage();
            page.setTeamId(team.getId());
            page.setUserId(user.getUser().getUserId());
            page.setType("0");
            List<MyOverviewPage> pageList=myOverviewPageService.selectMyOverviewPageList(page);
            team.setPageList(pageList);
        });
        return AjaxResult.success(list);
    }

    @GetMapping("/getPublicTeamlist")
    public AjaxResult getPublicTeamlist(MyOverviewTeam myOverviewTeam)
    {

        List<MyOverviewTeam> list = new ArrayList<>();
        MyOverviewTeam team=new MyOverviewTeam();
        team.setId(1L);
        team.setTeamName("公共概览");
        team.setSortNo(1L);
        MyOverviewPage page=new MyOverviewPage();
        page.setType("0");
        page.setPublicFlag("Y");
        List<MyOverviewPage> pageList=myOverviewPageService.selectMyOverviewPageList(page);
        team.setPageList(pageList);
        list.add(team);
        return AjaxResult.success(list);
    }

    /**
     * 导出我的概览列表
     */
    @PreAuthorize("@ss.hasPermi('overview:team:export')")
    @Log(title = "我的概览", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MyOverviewTeam myOverviewTeam)
    {
        List<MyOverviewTeam> list = myOverviewTeamService.selectMyOverviewTeamList(myOverviewTeam);
        ExcelUtil<MyOverviewTeam> util = new ExcelUtil<MyOverviewTeam>(MyOverviewTeam.class);
        return util.exportExcel(list, "我的概览数据");
    }

    /**
     * 获取我的概览详细信息
     */
    @PreAuthorize("@ss.hasPermi('overview:team:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(myOverviewTeamService.selectMyOverviewTeamById(id));
    }

    /**
     * 新增我的概览
     */
    @PreAuthorize("@ss.hasPermi('overview:team:add')")
    @Log(title = "我的概览", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyOverviewTeam myOverviewTeam)
    {
        return toAjax(myOverviewTeamService.insertMyOverviewTeam(myOverviewTeam));
    }

    @Log(title = "概览分组排序", businessType = BusinessType.UPDATE)
    @PostMapping("/setPageTeamOverview")
    public AjaxResult setPageTeamOverview(@RequestBody List<MyOverviewTeam> teamList)
    {
        return toAjax(myOverviewTeamService.setMyOverviewTeam(teamList));
    }

    /**
     * 修改我的概览
     */
    @PreAuthorize("@ss.hasPermi('overview:team:edit')")
    @Log(title = "我的概览", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyOverviewTeam myOverviewTeam)
    {
        return toAjax(myOverviewTeamService.updateMyOverviewTeam(myOverviewTeam));
    }

    /**
     * 删除我的概览
     */
    @PreAuthorize("@ss.hasPermi('overview:team:remove')")
    @Log(title = "我的概览", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(myOverviewTeamService.deleteMyOverviewTeamByIds(ids));
    }
}
